﻿using SD.Infrastructure.EntityBase;
using System;
using System.Text;

namespace SD.ContentSystem.Domain.Entities
{
    /// <summary>
    /// 文章
    /// </summary>
    public class Article : AggregateRootEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected Article()
        {
            //默认值
            this.Published = false;
            this.VisitCount = 0;
            this.FileName = string.Format("{0}.html", Guid.NewGuid());
            this.StoragePath = string.Format("/{0}/{1}/{2}/", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
        }
        #endregion

        #region 01.创建文章构造器
        /// <summary>
        /// 创建文章构造器
        /// </summary>
        /// <param name="channelNo">栏目编号</param>
        /// <param name="title">标题</param>
        /// <param name="subTitle">副标题</param>
        /// <param name="author">作者</param>
        /// <param name="source">来源</param>
        /// <param name="content">内容</param>
        /// <param name="imagePath">图片地址</param>
        /// <param name="thumbPath">缩略图</param>
        /// <param name="attachmentPath">附件</param>
        /// <param name="sort">排序</param>
        /// <param name="isTop">是否置顶</param>
        public Article(string channelNo, string title, string subTitle, string author, string source, string content, string imagePath, string thumbPath, string attachmentPath, int sort, bool isTop)
            : this()
        {
            this.ChannelNo = channelNo;
            base.Name = title;
            this.SubTitle = subTitle;
            this.Author = author;
            this.Source = source;
            this.Content = content;
            this.ImagePath = imagePath;
            this.ThumbPath = thumbPath;
            this.AttachmentPath = attachmentPath;
            this.Sort = sort;
            this.IsTop = isTop;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #endregion

        #region # 属性

        #region 栏目编号 —— string ChannelNo
        /// <summary>
        /// 栏目编号
        /// </summary>
        public string ChannelNo { get; private set; }
        #endregion

        #region 副标题 —— string SubTitle
        /// <summary>
        /// 副标题
        /// </summary>
        public string SubTitle { get; private set; }
        #endregion

        #region 作者 —— string Author
        /// <summary>
        /// 作者
        /// </summary>
        public string Author { get; private set; }
        #endregion

        #region 来源 —— string Source
        /// <summary>
        /// 来源
        /// </summary>
        public string Source { get; private set; }
        #endregion

        #region 内容 —— string Content
        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; private set; }
        #endregion

        #region 链接地址 —— string Url
        /// <summary>
        /// 链接地址
        /// </summary>
        public string Url { get; private set; }
        #endregion

        #region 图片路径 —— string ImagePath
        /// <summary>
        /// 图片路径
        /// </summary>
        public string ImagePath { get; private set; }
        #endregion

        #region 缩略图路径 —— string ThumbPath
        /// <summary>
        /// 缩略图路径
        /// </summary>
        public string ThumbPath { get; private set; }
        #endregion

        #region 附件路径 —— string AttachmentPath
        /// <summary>
        /// 附件路径
        /// </summary>
        public string AttachmentPath { get; private set; }
        #endregion

        #region 上一篇 —— string PrevPage
        /// <summary>
        /// 上一篇
        /// </summary>
        public string PrevPage { get; private set; }
        #endregion

        #region 下一篇 —— string NextPage
        /// <summary>
        /// 下一篇
        /// </summary>
        public string NextPage { get; private set; }
        #endregion

        #region 保存路径 —— string StoragePath
        /// <summary>
        /// 保存路径
        /// </summary>
        public string StoragePath { get; private set; }
        #endregion

        #region 文件名 —— string FileName
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get; private set; }
        #endregion

        #region 文章排序 —— int Sort
        /// <summary>
        /// 文章排序
        /// </summary>
        public int Sort { get; private set; }
        #endregion

        #region 是否置顶 —— bool IsTop
        /// <summary>
        /// 是否置顶
        /// </summary>
        public bool IsTop { get; private set; }
        #endregion

        #region 是否已发布 —— bool Published
        /// <summary>
        /// 是否已发布
        /// </summary>
        public bool Published { get; private set; }
        #endregion

        #region 发布时间 —— DateTime? PublishedTime
        /// <summary>
        /// 发布时间
        /// </summary>
        public DateTime? PublishedTime { get; private set; }
        #endregion

        #region 访问量 —— long VisitCount
        /// <summary>
        /// 访问量
        /// </summary>
        public long VisitCount { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 设置文章链接地址 —— void SetUrl(string url)
        /// <summary>
        /// 设置文章链接地址
        /// </summary>
        /// <param name="url">链接地址</param>
        public void SetUrl(string url)
        {
            this.Url = url;
        }
        #endregion

        #region 修改文章信息 —— void UpdateInfo(string title, string subTitle, string author...
        /// <summary>
        /// 修改文章信息
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="subTitle">副标题</param>
        /// <param name="author">作者</param>
        /// <param name="source">来源</param>
        /// <param name="content">内容</param>
        /// <param name="imagePath">图片地址</param>
        /// <param name="thumbPath">缩略图</param>
        /// <param name="attachmentPath">附件</param>
        /// <param name="sort">排序</param>
        public void UpdateInfo(string title, string subTitle, string author, string source, string content, string imagePath, string thumbPath, string attachmentPath, int sort)
        {
            base.Name = title;
            this.SubTitle = subTitle;
            this.Author = author;
            this.Source = source;
            this.Content = content;
            this.ImagePath = imagePath;
            this.ThumbPath = thumbPath;
            this.AttachmentPath = attachmentPath;
            this.Sort = sort;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #region 设置上一篇与下一篇 —— void SetPrevAndNextPage(string prevPage...
        /// <summary>
        /// 设置上一篇与下一篇
        /// </summary>
        /// <param name="prevPage">上一篇</param>
        /// <param name="nextPage">下一篇</param>
        public void SetPrevAndNextPage(string prevPage, string nextPage)
        {
            this.PrevPage = prevPage;
            this.NextPage = nextPage;
        }
        #endregion

        #region 置顶 —— void SetTop()
        /// <summary>
        /// 置顶
        /// </summary>
        public void SetTop()
        {
            this.IsTop = true;
        }
        #endregion

        #region 取消置顶 —— void CancelTop()
        /// <summary>
        /// 设置/取消置顶
        /// </summary>
        public void CancelTop()
        {
            this.IsTop = false;
        }
        #endregion

        #region 发布 —— void Publish()
        /// <summary>
        /// 发布
        /// </summary>
        public void Publish()
        {
            this.Published = true;
            this.PublishedTime = DateTime.Now;
        }
        #endregion

        #region 访问 —— void Visit()
        /// <summary>
        /// 访问
        /// </summary>
        public void Visit()
        {
            this.VisitCount++;
        }
        #endregion

        #region 初始化关键字 —— void InitKeywords()
        /// <summary>
        /// 初始化关键字
        /// </summary>
        private void InitKeywords()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(base.Name);
            builder.Append(this.SubTitle);
            builder.Append(this.Author);
            builder.Append(this.Source);
            builder.Append(this.Content);

            base.SetKeywords(builder.ToString());
        }
        #endregion

        #endregion
    }
}
